/*
 * Copyright (c) 2011 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#define USELUA

#ifdef USELUA
#include <ma.h>
#include <MAInterpreter/LuaInterpreter.h>
#include <MAInterpreter/InterpreterMoblet.h>
#include <MAInterpreter/System.h>
#include <conprint.h>
#include "MAHeaders.h"

using namespace MAInterpreter;

void runCTest()
{
	// Draw lots of rects, log how long time it takes.

	int timeStamp1 = maGetMilliSecondCount();

	int n = 0;
	while (n < 1000)
	{
	  maSetColor(n);
	  maFillRect(0, 0, 100, 100);
	  maUpdateScreen();
	  n = n + 1;
	}

	int timeStamp2 = maGetMilliSecondCount();

	lprintfln("C test took %i ms", timeStamp2 - timeStamp1);
}

int runLuaTest()
{
	LuaInterpreter* interpreter = new LuaInterpreter();
	if (!interpreter->initialize())
	{
		return -1;
	}

	// Important to evaluate the Lua program after the Moblet
	// has been created. The Lua code might do networking or
	// other tasks that require a Moblet to be created.
	int success = interpreter->evalResource(TEST1);
	if (!success)
	{
		return -1;
	}

	return 0;
}

extern "C" int MAMain()
{
	int i;

	for (i = 0; i < 10; ++i)
		runCTest();

	for (i = 0; i < 10; ++i)
		runLuaTest();

	return 0;
}
#endif

#ifndef USELUA
#include <ma.h>
#include <conprint.h>

void runCTest()
{
	// Draw lots of rects, log how long time it takes.

	int timeStamp1 = maGetMilliSecondCount();

	int n = 0;
	while (n < 1000)
	{
	  maSetColor(n);
	  maFillRect(0, 0, 100, 100);
	  maUpdateScreen();
	  n = n + 1;
	}

	int timeStamp2 = maGetMilliSecondCount();

	lprintfln("C test took %i ms", timeStamp2 - timeStamp1);
}

extern "C" int MAMain()
{
	runCTest();
	return 0;
}
#endif

/*

Test: Draw 1000 rectangles, test run 10 times.

Results:

MoRE (Micke's laptop):

C test:
4635
4597
4579
4618
4607
4626
4633
4619
4579
4588
Average: 4608,1

Lua test:
4708
4699
4789
4743
4734
4760
4697
4737
4689
4759
Average: 4731,5

Android (Nexus One):

C test:
16583
16548
16521
16468
16553
16533
16518
16469
16552
16453
Average: 16519,8

Lua test:
16455
16503
16463
16447
16436
16485
16442
16438
16453
16480
Average: 16460,2


Size test:

Lua version:

Libraries linked:
mastd.lib, MAUtil.lib, MAUI.lib, MAFS.lib, MobileLuaLib.lib

Dead code elimination OFF:
608236 10-Jun-111 13:59 LuaPerformanceTest.apk

Dead code elimination ON:
430337 10-Jun-111 13:46 LuaPerformanceTest.apk

C version:

Libraries linked:
mastd.lib, MAUtil.lib, MAUI.lib, MAFS.lib

Dead code elimination OFF:
393894 10-Jun-111 14:01 LuaPerformanceTest.apk

Dead code elimination ON:
196961 10-Jun-111 14:36 LuaPerformanceTest.apk

Libraries linked:
mastd.lib

Dead code elimination OFF:
243567 10-Jun-111 13:37 LuaPerformanceTest.apk

Dead code elimination ON:
193655 10-Jun-111 13:39 LuaPerformanceTest.apk


----------------

Various manual tests:

MoRE:
1000 rectangles:
[1] C test took 4143 ms
[1] Lua test took 4147 ms

[3] C test took 4765 ms
[4] C test took 4338 ms
[5] C test took 4778 ms

[7] Lua test took 3666 ms
[8] Lua test took 3920 ms
[9] Lua test took 4749 ms
[10] Lua test took 4740 ms
[11] Lua test took 4362 ms

5000 rectangles:
[2] C test took 22750 ms
[2] Lua test took 23103 ms

Android (Nexus One):

1000 rectangles:
I/maWriteLog( 3798): C test took 16571 ms
I/maWriteLog( 3798): Lua test took 16468 ms

I/maWriteLog( 3867): C test took 16593 ms
I/maWriteLog( 3879): C test took 16512 ms
I/maWriteLog( 3890): C test took 16550 ms

I/maWriteLog( 3923): Lua test took 16547 ms
I/maWriteLog( 3933): Lua test took 16549 ms
I/maWriteLog( 3964): Lua test took 16525 ms

5000 rectangles:
I/maWriteLog( 3830): C test took 82574 ms
I/maWriteLog( 3830): Lua test took 82386 ms

*/

